import { createRouter, createWebHistory } from 'vue-router'

// 白名单路由，不需要登录就可以访问
const whiteList = ['/login', '/register']

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'BasicLayout',
    component: () => import('@/layouts/BasicLayout.vue'),
    redirect: '/home/index',
    children: [
      {
        path: 'index',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页', keepAlive: true, requiresAuth: true }
      },
      {
        path: '/order',
        name: 'Order',
        component: () => import('@/views/order/index.vue'),
        meta: { title: '我的订单', keepAlive: true, requiresAuth: true }
      },
      {
        path: '/user',
        name: 'User',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '个人中心', keepAlive: true, requiresAuth: true }
      }
    ]
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/search/index.vue'),
    meta: { title: '搜索', requiresAuth: true }
  },
  {
    path: '/hospital',
    name: 'Hospital',
    component: () => import('@/views/hospital/index.vue'),
    meta: { title: '医院详情', requiresAuth: true }
  },
  {
    path: '/service',
    name: 'Service',
    component: () => import('@/views/service/index.vue'),
    meta: { title: '填写服务订单', requiresAuth: true }
  },
  {
    path: '/clients',
    name: 'Clients',
    component: () => import('@/views/clients/index.vue'),
    meta: { title: '客户管理', requiresAuth: true }
  },
  {
    path: '/order/detail',
    name: 'OrderDetail',
    component: () => import('@/views/order/detail.vue'),
    meta: { title: '订单详情', requiresAuth: true }
  },
  {
    path: '/order/server',
    name: 'OrderServer',
    component: () => import('@/views/order/server.vue'),
    meta: { title: '服务详情', requiresAuth: true }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register/index.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/update-user-info',
    name: 'UpdateUserInfo',
    component: () => import('@/views/updateUserInfo/index.vue'),
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/update-password',
    name: 'UpdatePassword',
    component: () => import('@/views/updatePassword/index.vue'),
    meta: {
      requiresAuth: true,
      title: '修改密码'
    }
  },
  {
    path: '/update-update-payPassword',
    name: 'UpdatePayPassword',
    component: () => import('@/views/updatePayPassword/index.vue'),
    meta: {
      requiresAuth: true,
      title: '设置支付密码'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置标题
  document.title = to.meta.title || '诊疗服务'
  
  // 获取token，判断用户是否已登录
  const token = localStorage.getItem('token')
  const userInfo = localStorage.getItem('userInfo')
  
  // 如果是白名单路由，直接放行
  if (whiteList.includes(to.path)) {
    next()
    return
  }
  
  // 如果已登录，允许访问
  if (token&&userInfo) {
    next()
    return
  }
  
  // 未登录且不在白名单中，重定向到登录页面
  next({
    path: '/login',
    query: { redirect: to.fullPath } // 将要访问的路径作为参数，登录成功后跳转回来
  })
})

export default router 